package com.mingqijia.gassafety.job.service.impl;

import com.mingqijia.gassafety.db.entity.WorkOrderCancelRecord;
import com.mingqijia.gassafety.db.mapper.WorkOrderCancelRecordMapper;
import com.mingqijia.gassafety.job.service.WorkOrderHandleService;
import com.mingqijia.gassafety.job.service.asst.AsstService;
import com.mingqijia.gassafety.job.service.mbp.TcisService;
import com.mingqijia.gassafety.shared.dto.OrderCancelDTO;
import com.mingqijia.gassafety.shared.dto.tcis.CancelBusiOrderDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 工单处理
 */
@Service
@Slf4j
public class WorkOrderHandleServiceImpl implements WorkOrderHandleService {

    @Resource
    AsstService asstWorkOrderService;

    @Resource
    TcisService tcisService;

    @Resource
    WorkOrderCancelRecordMapper workorderCancelRecordMapper;

    @Override
    public void cancelOrder(WorkOrderCancelRecord workOrderCancelRecord) {
        try {
            int workSystemType = workOrderCancelRecord.getWorkSystemType();
            String errMsg = "";
            try {
                if (workSystemType == 2) {//时刻助手-报警器独立流程
                    OrderCancelDTO orderCancelDTO = new OrderCancelDTO();
                    orderCancelDTO.setOrderNo(workOrderCancelRecord.getOrderNo());
                    orderCancelDTO.setFromSystem(4);
                    orderCancelDTO.setCancelReason(workOrderCancelRecord.getRemark());
                    orderCancelDTO.setSpId(workOrderCancelRecord.getSpId());
                    asstWorkOrderService.cancelOrderV2(orderCancelDTO);
                } else {//新时刻助手
                    CancelBusiOrderDTO cancelBusiOrderDTO = new CancelBusiOrderDTO();
                    cancelBusiOrderDTO.setPkid(workOrderCancelRecord.getOrderNo());
                    cancelBusiOrderDTO.setOptcode("ALARM");
                    cancelBusiOrderDTO.setRemark(workOrderCancelRecord.getRemark());
                    tcisService.tcisRequestCancelBusiOrder(cancelBusiOrderDTO, String.valueOf(workOrderCancelRecord.getConsumerId()), workOrderCancelRecord.getSpId());
                }
            } catch (Exception e) {
                log.error("工单取消失败1",e);
                errMsg = e.getMessage();
                if( StringUtils.isEmpty(errMsg) ){
                    errMsg = "工单取消失败";
                }
                errMsg = errMsg.substring(0,Math.min(errMsg.length(),250));
            }
            workOrderCancelRecord.setExecNum(workOrderCancelRecord.getExecNum()+1);
            if (StringUtils.isEmpty(errMsg)){
                workOrderCancelRecord.setResult(0);
            }else{
                workOrderCancelRecord.setResult(1);
                workOrderCancelRecord.setErrorMsg(errMsg);
            }
            workorderCancelRecordMapper.updateById(workOrderCancelRecord);
        }catch (Exception e){
            log.error("取消工单失败",e);
        }
    }
}
